package com.gxa.ibear.config;



import com.gxa.ibear.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 用于配置springmvc相关功能（<mvc:xxx></mvc:xxx>）
 * 1. 声明配置注解
 * 2. 实现一个接口 WebMvcConfigurer
 *
 * @author Lenovo
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 在boot中设置mvc的跨域
     * 并且通过注册器来提供具体的操作方法，注册器会自动注入到参数
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true)  //允许cookie使用
                .allowedOrigins("*");

    }

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/user/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:/user/img/upload");
//                .addResourceLocations("file:d:/img/upload/");
    }
}
